﻿using AutoMapper;
using MediatR;
using SmartCity.BasicData.Api.Application.Command.Accont;
using SmartCity.BasicData.Api.DTOs;
using SmartCity.BasicData.Domain.Entities;
using SmartCity.BasicData.ErrorCoce;
using SmartCity.BasicData.Infrastructure.Interface;

namespace SmartCity.BasicData.Api.Application.Handler.Accont
{
    public class GetDeptAccountHandler : IRequestHandler<GetDeptAccountCommand, ApiResult<List<AccountFromDepartmentDTO>>>
    {
        private readonly IBaseRepository<AccountModel> accRepo;
        private readonly IMapper mapper;

        public GetDeptAccountHandler(IBaseRepository<AccountModel> accRepo,IMapper mapper)
        {
            this.accRepo = accRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<List<AccountFromDepartmentDTO>>> Handle(GetDeptAccountCommand request, CancellationToken cancellationToken)
        {
            var deptAccounts = accRepo.GetAll().Where(x => x.DepartmentId == request.DepartmentId && x.IsDeleted == false);

            var dtos = mapper.Map<List<AccountFromDepartmentDTO>>(deptAccounts);

            //var linq = from account in deptAccounts
            //           select new AccountFromDepartmentDTO { AccountName=account.AccountName, Id=account.Id };

            return new ApiResult<List<AccountFromDepartmentDTO>>().Success("查询部门下所有账号成功", dtos);
        }
    }
}
